﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
	public class StringUtility
	{
		#region Join

		/// <summary>
		/// 在指定 IEnumerable 对象的每个元素之间串联指定的分隔符，从而产生单个串联的字符串。
		/// </summary>
		/// <param name="separator"></param>
		/// <param name="i"></param>
		/// <returns></returns>
		public static string Join(string separator, IEnumerable i)
		{
			return Join(separator, i, 0, int.MaxValue, string.Empty, string.Empty);
		}

		/// <summary>
		/// 在指定 IEnumerable 对象的每个元素之间串联指定的分隔符，从而产生单个串联的字符串。
		/// </summary>
		/// <param name="separator"></param>
		/// <param name="i"></param>
		/// <param name="itemFormat"></param>
		/// <returns></returns>
		public static string Join(string separator, IEnumerable i, string itemFormat)
		{
			return Join(separator, i, 0, int.MaxValue, itemFormat, string.Empty);
		}

		/// <summary>
		/// 在指定 IEnumerable 对象的每个元素之间串联指定的分隔符，从而产生单个串联的字符串。
		/// </summary>
		/// <param name="separator"></param>
		/// <param name="i"></param>
		/// <param name="itemFormat"></param>
		/// <param name="nullText"></param>
		/// <returns></returns>
		public static string Join(string separator, IEnumerable i, string itemFormat, string nullText)
		{
			return Join(separator, i, 0, int.MaxValue, itemFormat, nullText);
		}

		/// <summary>
		/// 在指定 IEnumerable 对象的每个元素之间串联指定的分隔符，从而产生单个串联的字符串。
		/// </summary>
		/// <param name="separator"></param>
		/// <param name="i"></param>
		/// <param name="startIndex"></param>
		/// <param name="count"></param>
		/// <returns></returns>
		public static string Join(string separator, IEnumerable i, int startIndex, int count)
		{
			return Join(separator, i, startIndex, count, string.Empty, string.Empty);
		}

		/// <summary>
		/// 在指定 IEnumerable 对象的每个元素之间串联指定的分隔符，从而产生单个串联的字符串。
		/// </summary>
		/// <param name="separator"></param>
		/// <param name="i"></param>
		/// <param name="startIndex"></param>
		/// <param name="count"></param>
		/// <param name="itemFormat"></param>
		/// <returns></returns>
		public static string Join(string separator, IEnumerable i, int startIndex, int count, string itemFormat)
		{
			return Join(separator, i, startIndex, count, itemFormat, string.Empty);
		}

		/// <summary>
		/// 在指定 IEnumerable 对象的每个元素之间串联指定的分隔符，从而产生单个串联的字符串。
		/// </summary>
		/// <param name="separator"></param>
		/// <param name="i"></param>
		/// <param name="startIndex"></param>
		/// <param name="count"></param>
		/// <param name="itemFormat"></param>
		/// <param name="nullText"></param>
		/// <returns></returns>
		public static string Join(string separator, IEnumerable i, int startIndex, int count, string itemFormat, string nullText)
		{
			if (null == i || count <= 0)
			{
				return string.Empty;
			}

			if(startIndex < 0)
			{
				throw new ArgumentOutOfRangeException("startIndex", startIndex, "起始索引必须大于或等于 0");
			}

			IEnumerator e = i.GetEnumerator();

			// 跳过前面的
			int index = 0;
			while(index < startIndex && e.MoveNext())
			{
				index++;
			}
			if(index != startIndex)
			{
				return string.Empty;
			}

			StringBuilder sb = new StringBuilder();
			int endIndex = startIndex + count;
			while(index < endIndex && e.MoveNext())
			{
				if(sb.Length != 0)
				{
					sb.Append(separator);
				}

				if(e.Current == null)
				{
					sb.Append(nullText);
				}
				else if (string.IsNullOrEmpty(itemFormat))
				{
					sb.Append(e.Current.ToString());
				}
				else
				{
					sb.AppendFormat(itemFormat, e.Current);
				}
			}

			return sb.ToString();
		}

		#endregion

		#region Repeat

		public static string Repeat(string item, int count)
		{
			StringBuilder sb = new StringBuilder();
			for(int index = 0; index < count; index ++)
			{
				sb.Append(item);
			}
			return sb.ToString();
		}

		#endregion
	}
}
